package com.houseliker.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;

import com.google.gson.Gson;
import com.houseliker.R;
import com.houseliker.listener.OnHeaderClickListener;
import com.houseliker.utils.StringUtils;
import com.houseliker.view.IHeaderLayout;
import com.houseliker.view.IToast;

/**
 * Created by sunrx on 2015/12/7.
 */
public abstract class BaseActivity extends AppCompatActivity {
    protected Context mContext;
    protected Gson gson;
    protected FragmentManager fragmentManager;
    protected FragmentTransaction transaction;

    private IHeaderLayout headerLayout;

    protected static final int TYPE_NORMAL = 0; //什么都没有
    protected static final int TYPE_HEADER = 1;//有头

    protected BaseActivity() {
        mContext = this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        gson = new Gson();
        fragmentManager = getSupportFragmentManager();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    protected void setView(int layoutResId, int type) {
        switch (type) {
            case TYPE_NORMAL:
                setContentView(layoutResId);
                break;
            default:
                headerLayout = new IHeaderLayout(this, layoutResId, type);
                setContentView(headerLayout);
                break;
        }
        if (null != headerLayout) {
            headerLayout.setHeaderClickListener(onHeaderClickListener);
        }
    }

    private OnHeaderClickListener onHeaderClickListener = new OnHeaderClickListener() {
        @Override
        public void onClickLeftButton() {
            onLeftButtonClick();
        }

        @Override
        public void onClickRightButton() {
            onRightButtonClick();
        }

        @Override
        public void onClickReLoadButton() {
            onReLoadButtonClick();
        }
    };

    protected void onLeftButtonClick() {
        //子类选择性实现
    }

    protected void onRightButtonClick() {
        //子类选择性实现
    }

    protected void onReLoadButtonClick() {
        //子类选择性实现
    }

    /**
     * 显示“返回”按钮和title
     *
     * @param titleText
     */
    public void setHeaderBar(String titleText) {
        if (null != headerLayout) {
            headerLayout.setHeaderBar(titleText);
        }
    }

    /**
     * 显示“返回”按钮、title、右边按钮。
     *
     * @param titleText
     * @param rightText
     */
    public void setHeaderBar(String titleText, String rightText) {
        if (null != headerLayout) {
            headerLayout.setHeaderBar(titleText, rightText);
        }
    }

    /**
     * 显示左边按钮、title、右边按钮
     *
     * @param leftText
     * @param titleText
     * @param rightText
     */
    public void setHeaderBar(String leftText, String titleText, String rightText) {
        if (null != headerLayout) {
            headerLayout.setHeaderBar(leftText, titleText, rightText);
        }
    }

    /**
     * 隐藏左边箭头
     */
    public void hideHeaderLeftArrow() {
        if (null != headerLayout) {
            headerLayout.hideHeaderLeftArrow();
        }
    }

    /**
     * 隐藏左边按钮
     */
    public void hideHeaderLeftButtom() {
        if (null != headerLayout) {
            headerLayout.hideHeaderLeftButtom();
        }
    }
    protected void C_Toast(String text) {
        if (StringUtils.isNullOrEmpty(text)) {
            return;
        }
        IToast.getIToast().setImage(R.mipmap.toast_warn).show(text);
    }

    protected void C_Toast(int txtResId) {
        IToast.getIToast().setImage(R.mipmap.toast_warn).show(txtResId);
    }
}
